////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.stock;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.BaseActivity;
import cc.telecomdigital.tdstock.activity.groups.StockGroup;
import cc.telecomdigital.tdstock.adapter.TwoTextColumnHolder;
import cc.telecomdigital.tdstock.adapter.WarrantCBBCAdapter;

/**
 * 
 * @author haibo
 * 
 *  for top bar button  caption  "search"
 *
 */
public class WarrantCBBCActivity extends BaseActivity
{
	private final int[]	RESOURCE_ARRAY_STRING_IDS = { 
			R.array.stock_wc_type, 
			R.array.stock_wc_put_call_warrant, 
			R.array.stock_wc_warrant_date_due, 
			R.array.stock_wc_warrant_price,
			R.array.stock_wc_premium_price, 
			R.array.stock_wc_total_process 
		};

	protected static final String		TAG				= WarrantCBBCActivity.class.getSimpleName();
	private static final long		START_ID			= 90000l;

	private TextView[]				_txtLeft;
	private TextView[]				_txtRight;
	private ListView				_listView;
	private Button					_toolBtnReturn;
	private ViewGroup[]				_viewGroup;

	private Button					_toolbutton_options;
	private WarrantCBBCAdapter		_adapter;
	private List<TwoTextColumnHolder>	_list;
	public static List<TwoTextColumnHolder> cViews = new ArrayList<TwoTextColumnHolder>();
	private TextView				_curr_stock_wc;

	private Context				_context;

	public static String			currStockCode;
	public static String			currStockName;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		try
		{
			setContentView(R.layout.warrant_cbbc);

			_curr_stock_wc = (TextView) findViewById(R.id.curr_stock_cw001);
			_curr_stock_wc.setText((((ITDLApplication) getApplicationContext()).getCurrentStockName()));

			_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
			_toolBtnReturn.setFocusable(true);
			_toolBtnReturn.setClickable(true);
			_toolBtnReturn.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					// BMP Mode  by ouyangzhihui 2011-10-13
					Class<? extends Activity>_cls = CurrentStockInfoActivity.class;
					if(((ITDLApplication)getApplicationContext()).getIsBMPMode())
					{
						_cls = BMPCurrentStockInfoActivity.class;
					}
					
					Intent intent = new Intent(getApplicationContext(), _cls);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);

					SwitchForwardActivity(_cls, intent);
				}
			});

			_toolbutton_options = (Button) findViewById(R.id.toolbtn_search);
			_toolbutton_options.setFocusable(true);
			_toolbutton_options.setClickable(true);
			_toolbutton_options.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
//					new SearchTask(getParent()).execute();
					String warranttype="",callput="",expiremonth="",exerprice="",premium="",percentout="";
					
					if (_list.get(0).item != 0)
						warranttype = getParamValue(0, _list.get(0).item);
					if (_list.get(1).item != 0)
						callput = getParamValue(1, _list.get(1).item);
					if (_list.get(2).item != 0)
						expiremonth = getParamValue(2, _list.get(2).item);
					if (_list.get(3).item != 0)
						exerprice = getParamValue(3, _list.get(3).item);
					if (_list.get(4).item != 0)
						premium = getParamValue(4, _list.get(4).item);
					if (_list.get(5).item != 0)
						percentout = getParamValue(5, _list.get(5).item);
					String[] iWParams = new String[] { warranttype, callput, expiremonth, exerprice, premium, percentout };
					SearchWarrantActivity.SetWarrantSearchParams(iWParams);
					SwitchForwardActivity(SearchWarrantActivity.class);
					
				}
			});

			_listView = (ListView) findViewById(R.id.warrant_cbbc_list);

			initListAndAdapter();
			initListView();
			initViewGroup();
		}
		catch (Exception e)
		{
			TDStockLog.e(TAG, e.getMessage() == null ? "" : e.getMessage());
		}

		_context = this;
	}

	protected Context getMyOwnContext()
	{
		return _context;
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		SearchWarrantActivity.isFromActivityBackToWarrantCBBC = false; // by ouyangzhihui 2011-6-15
	}

	@Override
	protected void onRestart()
	{
		super.onRestart();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		resetTitle(); //added by haibo, 2011-05-19
		_listView.invalidateViews();
	}

	private void initViewGroup()
	{
		_viewGroup = new ViewGroup[6];
		_viewGroup[0] = (ViewGroup) findViewById(R.id.rg01);
		_viewGroup[1] = (ViewGroup) findViewById(R.id.rg02);
		_viewGroup[2] = (ViewGroup) findViewById(R.id.rg03);
		_viewGroup[3] = (ViewGroup) findViewById(R.id.rg04);
		_viewGroup[4] = (ViewGroup) findViewById(R.id.rg05);
		_viewGroup[5] = (ViewGroup) findViewById(R.id.rg06);
		initRadioButtons();
		switchToViewGroup(0);
	}

	private void initRadioButtons()
	{
		int length = _viewGroup.length;
		final String[][] res = new String[length][];
		for (int index = 0; index < length; index++)
		{
			res[index] = this.getResources().getStringArray(RESOURCE_ARRAY_STRING_IDS[index]);
			if (index == 5)
			{
				TDStockLog.d(TAG, "index" + index + ":" + TextUtils.join(" ,", res[index]));
			}
		}
		for (int index = 0; index < length; index++)
		{
			RadioGroup rg = (RadioGroup) _viewGroup[index].findViewById(R.id.radiogroup01);
			int count = rg.getChildCount();
			if (count != res[index].length)
			{
				TDStockLog.e(TAG, "The setting on radio button is wrong( button  number not equals resource number)!");
				break;
			}

			for (int item = 0; item < count; item++)
			{
				if (index == 5)
				{
					TDStockLog.d(TAG, "item:" + item + ":" + res[index][item]);
				}
				RadioButton rbtn = (RadioButton) rg.getChildAt(item);
				rbtn.setText(res[index][item]);
				final int theIndex = index;
				final int theItem = item;
				rbtn.setOnCheckedChangeListener(new OnCheckedChangeListener()
				{
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
					{
						if (isChecked)
						{
							TwoTextColumnHolder view = cViews.get(theIndex);
							view.item = theItem;
							view.right.setText(res[theIndex][theItem]);
							
							_list.get(theIndex).item = theItem;
							
							/*_list.get(theIndex).item = theItem;
							_list.get(theIndex).right.setText(res[theIndex][theItem]);
//							//_adapter.notifyDataSetChanged();
							_adapter.notifyDataSetInvalidated();*/
						}
					}
				});
			}
		}
	}

	private void switchToViewGroup(int index)
	{
		int length = _viewGroup.length;
		for (int item = 0; item < length; item++)
		{
			if (item == index && _viewGroup[item] != null)
			{
				_viewGroup[item].setVisibility(View.VISIBLE);
			}
			else if (_viewGroup[item] != null)
			{
				_viewGroup[item].setVisibility(View.GONE);
			}
		}
	}

	private void initListAndAdapter()
	{
		final String[] txtRes = new String[6];
		int listSize = txtRes.length;
		_txtLeft = new TextView[listSize];
		_txtRight = new TextView[listSize];
		for (int index = 0; index < listSize; index++)
		{
			TextView right = new TextView(this);
			right.setText(R.string.stock_wc_selectall);
			_txtRight[index] = right;
			TextView left = new TextView(this);
			_txtLeft[index] = left;
		}
		_txtLeft[0].setText(R.string.stock_wc_type);
		_txtLeft[1].setText(R.string.stock_wc_put_get_warrant);
		_txtLeft[2].setText(R.string.stock_wc_date_due);
		_txtLeft[3].setText(R.string.stock_wc_price);
		_txtLeft[4].setText(R.string.stock_wc_premium_price);
		_txtLeft[5].setText(R.string.stock_wc_total_process);
		_list = new ArrayList<TwoTextColumnHolder>();
		long id = START_ID;
		for (int index = 0; index < listSize; index++)
		{
			id += index;
			TwoTextColumnHolder viewHolder = new TwoTextColumnHolder();
			viewHolder.id = id;
			viewHolder.left = _txtLeft[index];
			viewHolder.right = _txtRight[index];
			_list.add(viewHolder);
		}

		_adapter = new WarrantCBBCAdapter(LayoutInflater.from(this), _list);

	}
	private View lastView;
	private int[]				rowBgIds	= new int[] {
			R.drawable.first_item_selected_bg, R.drawable.ordinary_item_selected_bg,
			R.drawable.last_item_selected_bg		};

	private void initListView()
	{
		_listView.setAdapter(_adapter);

		_listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View currView, final int position, long id)
			{
				if (position != -1)
				{
					if(lastView != null)
					{
						lastView.setBackgroundDrawable(null);
					}
					if(position == 0)
					{
						currView.setBackgroundResource(rowBgIds[0]);
					}
					else if(position == _list.size()-1)
					{
						currView.setBackgroundResource(rowBgIds[rowBgIds.length-1]);
					}
					else
					{
						currView.setBackgroundResource(rowBgIds[1]);
					}
					lastView = currView;
					
					Runnable job = new Runnable()
					{
						public void run()
						{
							switchToViewGroup(position);
						}
					};

					if (_messageHandler != null)
						_messageHandler.post(job);
											
//					final Runnable job = new Runnable()
//					{
//						public void run()
//						{
//							switchToViewGroup(position);
//						}
//					};
//
//					new Thread()
//					{
//						public void run()
//						{
//							if (_messageHandler != null)
//								_messageHandler.post(job);
//						}
//					}.start();
				}
			}
		});
	}

	private String getParamValue(int index, int item)
	{
		String result = null;
		switch (index)
		{
			case 0:
				switch (item)
				{
					case 1:
						result = "Warrant";
						break;
					case 2:
						result = "BullBearWarrant";
						break;
				}
				break;
			case 1:
				switch (item)
				{
					case 1:
						result = "CallWarrant";
						break;
					case 2:
						result = "PutWarrant";
						break;
				}
				break;
			case 2:
				switch (item)
				{
					case 1:
						result = "0-3";
						break;
					case 2:
						result = "3-6";
						break;
					case 3:
						result = "6-12";
						break;
				}
				break;
			case 3:
				switch (item)
				{
					case 1:
						result = "InTheMoneyWarrant";
						break;
					case 2:
						result = "OutOfTheMoneyWarrant";
						break;
				}
				break;
			case 4:
				switch (item)
				{
					case 1:
						result = "0";
						break;
					case 2:
						result = "0-3";
						break;
					case 3:
						result = "3-6";
						break;
					case 4:
						result = "6-9";
						break;
					case 5:
						result = "9";
						break;
				}
				break;
			case 5:
				switch (item)
				{
					case 1:
						result = "10";
						break;
					case 2:
						result = "10-30";
						break;
					case 3:
						result = "30-50";
						break;
					case 4:
						result = "50-80";
						break;
					case 5:
						result = "80";
						break;
				}
		}
		return result;
	}

	private void resetTitle()
	{
		_curr_stock_wc.setText(currStockName);
		_curr_stock_wc.postInvalidate();
	}
	
	
	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return StockGroup.GetActivityGroupHostControl();
	}
}
